package com.ncmmall.mall.controller.business.supplier;

import com.ncmmall.domain.business.cloudstore.CloudStoreGoods;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Nullable;
import java.util.Map;

/**
 * @author linxs
 * @Description: 云仓管理
 * @Date: 2020-04-29 23:45
 */
@Controller
@RequestMapping(value = "/mall/cloudstore")
public class CloudStoreController {

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, PageParams pageParams, WebUser merchant) {

        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();

        String hql = builderSQL("select distinct d.supplier from CloudStoreGoods d where d.status = 1  ", params, merchant);
        QueryPage<WebUser> page = queryChannelService.findPage(hql, pageRequest.setParams(params), WebUser.class);

        page.setResult(Lists.transform(page.getResult(), new Function<WebUser, WebUser>() {
            @Nullable
            @Override
            public WebUser apply(@Nullable WebUser input) {

                double quantity = queryChannelService.selectSum("select ifnull(sum(d.quantity), 0) from mall_cloud_store_goods d where d.supplier_id = :mid and d.status = 1 ", ImmutableMap.of("mid", input.getId()));

                input.setGoodsNumber(new Double(quantity).intValue());

                return input;
            }
        }));

        model.addAttribute("page", page).addAttribute("params", merchant);

        return "mall/cloudStore/list";
    }

    /**
     * 查看详情
     */
    @RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
    public String details(Model model, @PathVariable("id") long id, PageParams pageParams, CloudStoreGoods detail) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        params.put("mid", id);

        String hql = builderOrderDetailSQL("select d from CloudStoreGoods d where  d.supplier.id = :mid and d.status = 1 ", params, detail);
        QueryPage<CloudStoreGoods> page = queryChannelService.findPage(hql, pageRequest.setParams(params), CloudStoreGoods.class);

        detail.setId(id);
        model.addAttribute("page", page).addAttribute("params", detail);

        return "mall/cloudStore/details";
    }

    private String builderOrderDetailSQL(String sql, Map<String, Object> params, CloudStoreGoods detail) {
        return sql;
    }

    private String builderSQL(String sql, Map<String, Object> params, WebUser merchant) {

        if (StringUtils.isNotBlank(merchant.getShopName())) {
            sql += " and d.supplier.shopName like :shopName";
            params.put("shopName", "%" + merchant.getShopName() + "%");
        }

        if (StringUtils.isNotBlank(merchant.getRealName())) {
            sql += " and d.supplier.realName like :realName";
            params.put("realName", "%" + merchant.getRealName() + "%");
        }

        if (StringUtils.isNotBlank(merchant.getMobile())) {
            sql += " and d.supplier.mobile = :mobile";
            params.put("mobile", merchant.getMobile());
        }
        return sql;
    }

}
